package com.zhaoyd.uaademo.config.security;

import com.zhaoyd.uaademo.entity.SysUser;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

/**
 * SessionUser工具类，获取当前登录的用户
 * @author zhaoyd
 * @date 2020-10-31 18:01
 */
public class SessionUserUtils {


    public static MyUserDetail getUserDetail(){
        MyUserDetail currentUser = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof MyUserDetail){
            currentUser =  (MyUserDetail) principal;
            //清空密码
            currentUser.setPassword(null);
            currentUser.getSysUser().setPassword(null);
        }
        return currentUser;
    }

    public static SysUser getCurrentUser(){
        MyUserDetail myUserDetail = getUserDetail();
        SysUser sysUser = null;
        if(myUserDetail != null){
            sysUser = myUserDetail.getSysUser();
        }
        return sysUser;
    }
}
